package top.kaoshanji.p2ch17a;

import top.kaoshanji.y1constant.FileConstant;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Random;

/**
 * 代码清单-p2ch17a-544页a：向文件写入数据
 * author kaoshanji
 * ^_^
 * create time 2025/3/11 17:35
 */
public class Writing {

    static Random rand = new Random(47);
    static final int SIZE = 1000;
    static String path = FileConstant.LOCAL_ROOT_PATH.concat("p2ch17a544页a\\");

    public static void main(String[] args) throws IOException {

        byte[] bytes = new byte[SIZE];
        rand.nextBytes(bytes);

        // 将字节写入一个文件
        Path bytesPath = Paths.get(path.concat("bytes.dat"));
        Files.write(bytesPath, bytes);
        System.out.println("bytes.dat: " + Files.size(bytesPath));

        Path linesPath = Paths.get(path.concat("Cheese.txt"));

        // 将实现了 Iterable 接口的类的对象写入一个文件
        List<String> lines = Files.readAllLines(Paths.get(path.concat("Cheese.dat")));
        Files.write(linesPath, lines);
        System.out.println("Cheese.txt: " + Files.size(linesPath));

    }


}
